﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeradorMassaDados.Bovespa.Business;
using System.Text.RegularExpressions;

namespace GeradorMassaDados.Bovespa.Shell
{
    public partial class frmDataGenerator : Form
    {
        #region Properties

        public CollectItem Item { get; set; }

        public List<SQLCommandLog> CommandLogs { get; set; }

        #endregion

        #region Constructor

        public frmDataGenerator()
        {
            InitializeComponent();

            try
            {
                CleanUp();
                LoadData();
                lvwCommandLog.Columns[0].Width = lvwCommandLog.Width - 1;
                saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, this.Text + " capotou", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        #endregion

        #region Methods

        #region Events

        private void btnStartCollecting_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(cbbUserName.Text))
                {
                    MessageBox.Show(this, "Informe um usuário de rede.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                this.StartCollecting(new DataAcess().GetOracleDate());
            }
            catch (Exception ex) { MessageBox.Show(this, ex.ToString(), this.Text + " capotou", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnEndCollecting_Click(object sender, EventArgs e)
        {
            try
            {
                EndCollecting();
            }
            catch (Exception ex) { MessageBox.Show(this, ex.ToString(), this.Text + " capotou", MessageBoxButtons.OK, MessageBoxIcon.Error); }


        }

        private void btnGenerateData_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.OK == saveFileDialog.ShowDialog(this))
                {
                    this.GenerateData();
                    MessageBox.Show(this, "Arquivo gerado com sucesso", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    CleanUp();
                }
                else MessageBox.Show(this, "Arquivo não gerado", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex) { MessageBox.Show(this, ex.ToString(), this.Text + " capotou", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnCleanUp_Click(object sender, EventArgs e)
        {
            try
            {
                CleanUp();
            }
            catch (Exception ex) { MessageBox.Show(this, ex.ToString(), this.Text + " capotou", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        #endregion

        #region Private

        private void StartCollecting(DateTime startTime)
        {
            Item = new CollectItem(startTime, cbbUserName.Text);
            txtStartTime.Text = string.Concat(Item.StartTime.ToString("dd/MM/yyyy hh:mm:ss"));

            btnGenerateData.Enabled = cbbUserName.Enabled = btnStartCollecting.Enabled = false;
            btnEndCollecting.Enabled = true;
            Item.Save();
            MessageBox.Show(this, "Data de inicio capturada", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void EndCollecting()
        {
            btnEndCollecting.Enabled = cbbUserName.Enabled = btnStartCollecting.Enabled = false;
            btnGenerateData.Enabled = true;
            Item.SetEndTime();
            cbbUserName.SelectedIndex = cbbUserName.FindString(Item.Terminal);
            txtStartTime.Text = string.Concat(Item.StartTime.ToString("dd/MM/yyyy hh:mm:ss"));
            txtEndTime.Text = string.Concat(Item.EndTime.ToString("dd/MM/yyyy hh:mm:ss"));

            CommandLogs = new DataCollectorBiz().GetCommandLogs(Item);

            foreach (var item in CommandLogs)
                lvwCommandLog.Items.Add(item.SQLFormated);

            Item.Save();
            MessageBox.Show(this, "Dados capturados", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void GenerateData()
        {
            try
            {
                new DataCollectorBiz().Save(this.CommandLogs, saveFileDialog.FileName);
            }
            finally { CleanUp(); }
        }

        private void CleanUp()
        {
            btnGenerateData.Enabled = btnEndCollecting.Enabled = false;
            btnStartCollecting.Enabled = true;
            txtEndTime.Text = txtStartTime.Text = string.Empty;
            lvwCommandLog.Items.Clear();

#if !DEBUG
            cbbUserName.Enabled = false;
#else
            cbbUserName.Enabled = true;
#endif

            CommandLogs = null;
            if (Item != null) Item.Delete();
            Item = null;            
        }

        private void LoadData()
        {
            cbbUserName.DataSource = new DataCollectorBiz().GetUserNames();

            Item = CollectItem.Load();
            if (null != Item  && -1 < cbbUserName.Items.IndexOf(Item.Terminal))
            {
                cbbUserName.SelectedItem = Item.Terminal;
                if(DialogResult.Yes == MessageBox.Show(this, "Você começou um processo que não teve o arquivo gerado você deseja continuar do ponto parado?",
                    this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    if (DateTime.MinValue != Item.EndTime)                    
                        EndCollecting();
                    else                    
                        StartCollecting(Item.StartTime);
                }
            }

        }

        #endregion

        #endregion
    }
}